package servlet;

import bean.Result;
import biz.CartBiz;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "ModifyCartServlet", value = "/ModifyCartServlet.s")
public class ModifyCartServlet extends HttpServlet {
    private CartBiz biz = new CartBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Result result;
        try {
            String[] products = request.getParameterValues("product");
            String[] amounts = request.getParameterValues("amount");

            HttpSession session = request.getSession();
            System.out.println(session.getAttribute("userid"));
            String uid = String.valueOf(session.getAttribute("userid"));
            System.out.println(amounts[0]);
            for (int i = 0; i < products.length; i++){
                if (!"0".equals(amounts[i])){
                    biz.modify(uid, products[i], amounts[i]);
                }
            }
            result = new Result(1, "修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(0, "修改失败,商品数量不足", null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
